﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: Кулик
 * Дата: 31.03.2011
 * Время: 11:51
 * 
 * Для изменения этого шаблона используйте Сервис | Настройка | Кодирование | Правка стандартных заголовков.
 */
using System;
using System.Diagnostics;
using NLog;

namespace Video.Mov
{
	/// <summary>
	/// Description of Decoder.
	/// </summary>
	public class Decoder
	{
		Logger log;
		VideoStream videoStream;
		
		public Decoder(string inFilePath)
		{
			videoStream = new VideoStream(inFilePath);
			log =  LogManager.GetCurrentClassLogger();
		}
		
		public Atom ReadAtom(){
			Atom temp = new Atom();
			temp.Size = videoStream.avio_rb32();
			temp.Type = videoStream.inVideoStream.ReadChars(4);
			string s = new String(temp.Type);
			
			Console.WriteLine(s);
			
			switch (s) {
				case "moov":
					log.Info("----MOOV----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					break;
				case "trak":
					log.Info("----TRAK----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					break;
				case "edts":
					log.Info("----EDTS----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					break;
				case "mdia":
					log.Info("----MDIA----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					break;
				case "minf":
					log.Info("----MINF----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					break;
				case "dinf":
					log.Info("----DINF----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					break;
				case "stbl":
					log.Info("----STBL----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					break;
				case "free":
					log.Info("----FREE----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					byte[] t = videoStream.inVideoStream.ReadBytes((int)(temp.Size)-8);
					break;
				case "mdat":
					log.Info("----MDAT----");
					log.Debug("Size: " + temp.Size);
					log.Debug("Type: " + new String(temp.Type));
					byte[] t1 = videoStream.inVideoStream.ReadBytes((int)(temp.Size)-8);
					return temp; //это последний атом
					break;
				case "stsd":
					ReadStsd(temp);
					break;
				case "stco":
					ReadStco(temp);
					break;
				case "stsc":
					ReadStsc(temp);
					break;
				case "stss":
					ReadStss(temp);
					break;
				case "stsz":
					ReadStsz(temp);
					break;
				case "stts":
					ReadStts(temp);
					break;
				case "dref":
					ReadDref(temp);
					break;
				case "smhd":
					ReadSmhd(temp);
					break;
				case "vmhd":
					ReadVmhd(temp);
					break;
				case "mdhd":
					ReadMdhd(temp);
					break;
				case "hdlr":
					ReadHdlr(temp);
					break;
				case "tkhd":
					ReadTkhd(temp);
					break;
				case "elst":
					ReadElst(temp);
					break;
				case "mvhd":
					ReadMvhd(temp);
					break;
				case "ftyp":
					ReadFtyp(temp);
					break;
				default:
					//если неизвестный атом то выходим
					return null;
			}
			ReadAtom();
			return null;
		}
		
		public MovieHeaderAtom ReadMvhd(Atom atom){
			MovieHeaderAtom temp = new MovieHeaderAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.CreationTime = videoStream.avio_rb32();
			temp.ModificationTime = videoStream.avio_rb32();
			temp.TimeScale = videoStream.avio_rb32();
			temp.Duration = videoStream.avio_rb32();
			temp.PreferredRate = videoStream.avio_rb32();
			temp.PreferredVolume = videoStream.avio_rb16();
			temp.Reserved = videoStream.inVideoStream.ReadBytes(10);
			temp.MatrixStructure = videoStream.inVideoStream.ReadBytes(36);
			temp.PreviewTime = videoStream.avio_rb32();
			temp.PreviewDuration = videoStream.avio_rb32();
			temp.PosterTime = videoStream.avio_rb32();
			temp.SelectionTime = videoStream.avio_rb32();
			temp.SelectionDuration = videoStream.avio_rb32();
			temp.CurrentTime = videoStream.avio_rb32();
			temp.NextTrackID = videoStream.avio_rb32();
			
			log.Info("----MVHD----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("CreationTime: " + temp.CreationTime);
			log.Debug("ModificationTime: " + temp.ModificationTime);
			log.Debug("TimeScale: " + temp.TimeScale);
			log.Debug("Duration: " + temp.Duration);
			log.Debug("PreferredRate: " + temp.PreferredRate);
			log.Debug("PreferredVolume: " + temp.PreferredVolume);
			log.Debug("Reserved: " + temp.Reserved);
			log.Debug("MatrixStructure: " + temp.MatrixStructure);
			log.Debug("PreviewTime: " + temp.PreviewTime);
			log.Debug("PreviewDuration: " + temp.PreviewDuration);
			log.Debug("PosterTime: " + temp.PosterTime);
			log.Debug("SelectionTime: " + temp.SelectionTime);
			log.Debug("SelectionDuration: " + temp.SelectionDuration);
			log.Debug("CurrentTime: " + temp.CurrentTime);
			log.Debug("NextTrackID: " + temp.NextTrackID);
			
			return temp;
		}
		
		public MediaHeaderAtom ReadMdhd(Atom atom){
			MediaHeaderAtom temp = new MediaHeaderAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.CreationTime = videoStream.avio_rb32();
			temp.ModificationTime = videoStream.avio_rb32();
			temp.TimeScale = videoStream.avio_rb32();
			temp.Duration = videoStream.avio_rb32();
			temp.Language = videoStream.avio_rb16();
			temp.Quality = videoStream.avio_rb16();
			
			log.Info("----MDHD----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("CreationTime: " + temp.CreationTime);
			log.Debug("ModificationTime: " + temp.ModificationTime);
			log.Debug("TimeScale: " + temp.TimeScale);
			log.Debug("Duration: " + temp.Duration);
			log.Debug("Language: " + temp.Language);
			log.Debug("Quality: " + temp.Quality);
			
			return temp;
		}
		
		public HandlerReferenceAtom ReadHdlr(Atom atom){
			HandlerReferenceAtom temp = new HandlerReferenceAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.ComponentType = videoStream.inVideoStream.ReadChars(4);
			temp.ComponentSubtype = videoStream.inVideoStream.ReadChars(4);
			temp.ComponentManufactured = videoStream.avio_rb32();
			temp.ComponentFlags = videoStream.avio_rb32();
			temp.ComponentFlagsMask = videoStream.avio_rb32();
			temp.ComponentName =  videoStream.readPascalString();
			
			log.Info("----HDLR----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("ComponentType: " + new String(temp.ComponentType));
			log.Debug("ComponentSubtype: " + new String(temp.ComponentSubtype));
			log.Debug("ComponentManufactured: " + temp.ComponentManufactured);
			log.Debug("ComponentFlags: " + temp.ComponentFlags);
			log.Debug("ComponentFlagsMask: " + temp.ComponentFlagsMask);
			log.Debug("ComponentName: " + temp.ComponentName);
			
			return temp;
		}
		
		public SoundMediaInformationHeaderAtom ReadSmhd(Atom atom){
			SoundMediaInformationHeaderAtom temp = new SoundMediaInformationHeaderAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.Balance = videoStream.avio_rb16();
			temp.Reserved = videoStream.avio_rb16();
			
			
			log.Info("----SMHD----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("Balance: " + temp.Balance);
			log.Debug("Reserved: " + temp.Reserved);
			
			
			return temp;
		}
		
		public VideoMediaInformationHeaderAtom ReadVmhd(Atom atom){
			VideoMediaInformationHeaderAtom temp = new VideoMediaInformationHeaderAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.GraphicsMode = videoStream.avio_rb16();
			temp.Opcolor = videoStream.inVideoStream.ReadBytes(6);
			
			
			log.Info("----VMHD----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("GraphicsMode: " + temp.GraphicsMode);
			log.Debug("Opcolor: " + temp.Opcolor);
			
			
			return temp;
		}
		
		public TrackHeaderAtom ReadTkhd(Atom atom){
			TrackHeaderAtom temp = new TrackHeaderAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.CreationTime = videoStream.avio_rb32();
			temp.ModificationTime = videoStream.avio_rb32();
			temp.TrackID = videoStream.avio_rb32();
			temp.Reserved = videoStream.avio_rb32();
			temp.Duration = videoStream.avio_rb32();
			temp.Reserved2 = videoStream.avio_rb64();
			temp.Layer = videoStream.avio_rb16();
			temp.AlternateGroup = videoStream.avio_rb16();
			temp.Volume = videoStream.avio_rb16();
			temp.Reserved3 = videoStream.avio_rb16();
			
			temp.MatrixStructure = videoStream.inVideoStream.ReadBytes(36);
			temp.TrackWidth = videoStream.avio_rb32();
			temp.TrackHeight = videoStream.avio_rb32();
			
			log.Info("----TKHD----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("CreationTime: " + temp.CreationTime);
			log.Debug("ModificationTime: " + temp.ModificationTime);
			log.Debug("TrackID: " + temp.TrackID);
			log.Debug("Reserved: " + temp.Reserved);
			log.Debug("Duration: " + temp.Duration);
			log.Debug("Reserved2: " + temp.Reserved2);
			log.Debug("Layer: " + temp.Layer);
			log.Debug("AlternateGroup: " + temp.AlternateGroup);
			log.Debug("Volume: " + temp.Volume);
			log.Debug("Reserved3: " + temp.Reserved3);
			log.Debug("MatrixStructure: " + temp.MatrixStructure);
			log.Debug("TrackWidth: " + temp.TrackWidth);
			log.Debug("TrackHeight: " + temp.TrackHeight);
			
			return temp;
		}
		
		public FtypAtom ReadFtyp(Atom atom){
			FtypAtom temp = new FtypAtom(atom);
			temp.MajorBrand = videoStream.inVideoStream.ReadChars(4);
			temp.Ver = videoStream.avio_rb32();
			temp.CompatibleBand1 = videoStream.inVideoStream.ReadChars(4);
			temp.CompatibleBand2 = videoStream.inVideoStream.ReadChars(4);
			
			log.Info("----FTYP----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("MajorBrand: " + new String(temp.MajorBrand));
			log.Debug("Ver: " + temp.Ver);
			log.Debug("CompatibleBand1: " + new String(temp.CompatibleBand1));
			log.Debug("CompatibleBand2: " + new String(temp.CompatibleBand2));
			return temp;
		}
		
		public EditListAtom ReadElst(Atom atom){
			EditListAtom temp = new EditListAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----ELST----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("NumberOfEntries: " + temp.NumberOfEntries);
			
			for (int i = 0; i < temp.NumberOfEntries; i++) {
				EditListItem tempItem = new EditListItem();
				tempItem.TrackDuration = videoStream.avio_rb32();
				tempItem.MediaTime = videoStream.avio_rb32();
				tempItem.MediaRate = videoStream.avio_rb32();
				temp.EditListTable.Add(tempItem);
				
				log.Info("--Edit item--");
				log.Debug("TrackDuration: " + tempItem.TrackDuration);
				log.Debug("MediaTime: " + tempItem.MediaTime);
				log.Debug("MediaRate: " + tempItem.MediaRate);
			}
			
			return temp;
		}
		
		public Atom ReadDref(Atom atom){
			//Atom temp = new Atom(atom);
			atom.Version = videoStream.avio_r8();
			atom.Flags = videoStream.inVideoStream.ReadBytes(3);
			atom.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----DREF----");
			log.Debug("Size: " + atom.Size);
			log.Debug("Type: " + new String(atom.Type));
			log.Debug("Version: " + atom.Version);
			log.Debug("Flags: " + atom.Flags);
			log.Debug("NumberOfEntries: " + atom.NumberOfEntries);
			
			for (int i = 0; i < atom.NumberOfEntries; i++) {
				Atom tempItem = new Atom();
				tempItem.Size = videoStream.avio_rb32();
				tempItem.Type = videoStream.inVideoStream.ReadChars(4);
				tempItem.Version = videoStream.avio_r8();
				tempItem.Flags = videoStream.inVideoStream.ReadBytes(3);
				tempItem.Data = new byte[tempItem.Size-12];
				tempItem.Data = videoStream.inVideoStream.ReadBytes((int)(tempItem.Size)-12);
				atom.Child.Add(tempItem);
				
				log.Info("--Data references--");
				log.Debug("Size: " + tempItem.Size);
				log.Debug("Type: " + new String(tempItem.Type));
				log.Debug("Version: " + tempItem.Version);
				log.Debug("Flags: " + tempItem.Flags);
			}
			
			return atom;
		}
		
		public SampleDescriptionAtom ReadStsd(Atom atom){
			SampleDescriptionAtom temp = new SampleDescriptionAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----Stsd----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("NumberOfEntries: " + temp.NumberOfEntries);
			
			for (int i = 0; i < temp.NumberOfEntries; i++) {
				SampleDescriptionTableItem tempItem = new SampleDescriptionTableItem();
				tempItem.Size = videoStream.avio_rb32();
				tempItem.DataFormat = videoStream.inVideoStream.ReadChars(4);
				tempItem.Reserved = videoStream.inVideoStream.ReadBytes(6);
				tempItem.DataReferenceIndex = videoStream.avio_rb16();
				string s = new String(tempItem.DataFormat);
				
				switch (s) {
					case "samr":
						AMRSampleEntry amr = new AMRSampleEntry(tempItem);
						amr.Reserver = videoStream.inVideoStream.ReadBytes(8);
						amr.Reserver2 = videoStream.avio_rb16();
						amr.Reserver3 = videoStream.avio_rb16();
						amr.Reserver4 = videoStream.avio_rb32();
						amr.TimeScale = videoStream.avio_rb16();
						amr.Reserver5 = videoStream.avio_rb16();
						
						//daml
						amr.BoxHeaderSize = videoStream.avio_rb32();
						amr.BoxHeaderType = videoStream.inVideoStream.ReadChars(4);
						
						amr.vendor = videoStream.inVideoStream.ReadChars(4);
						amr.decoder_version = videoStream.avio_r8();
						amr.mode_set = videoStream.avio_rb16();
						amr.mode_change_period = videoStream.avio_r8();
						amr.frames_per_sample = videoStream.avio_r8();
						
						temp.sampleDescriptionTable.Add(amr);
						
						log.Info("--amr sample item--");
						log.Debug("Size: " + amr.Size);
						log.Debug("DataFormat: " + new String(amr.DataFormat));
						log.Debug("Reserved: " + amr.Reserved);
						log.Debug("DataReferenceIndex: " + amr.DataReferenceIndex);
						log.Debug("TimeScale: " + amr.TimeScale);
						log.Debug("BoxHeaderSize: " +amr.BoxHeaderSize);
						log.Debug("BoxHeaderType: " + new String(amr.BoxHeaderType));
						log.Debug("vendor: " + new String(amr.vendor));
						
						log.Debug("decoder_version: " + amr.decoder_version);
						log.Debug("mode_set: " +amr.mode_set);
						log.Debug("mode_change_period: " + amr.mode_change_period);
						log.Debug("frames_per_sample: " + amr.frames_per_sample);
						
						break;
					case "mp4v":
						MP4VisualSampleEntry mp4v = new MP4VisualSampleEntry(tempItem);
						mp4v.Reserver = videoStream.inVideoStream.ReadBytes(16);
						mp4v.Width = videoStream.avio_rb16();
						mp4v.Height = videoStream.avio_rb16();
						mp4v.Reserver2 = videoStream.avio_rb32();
						mp4v.Reserver3 = videoStream.avio_rb32();
						mp4v.Reserver4 = videoStream.avio_rb32();
						mp4v.Reserver5 = videoStream.avio_rb16();
						mp4v.Reserver6 = videoStream.inVideoStream.ReadBytes(32);
						mp4v.Reserver7 = videoStream.avio_rb16();
						mp4v.Reserver8 = videoStream.avio_rb16();
						
						//esds
						mp4v.BoxHeaderSize = videoStream.avio_rb32();
						mp4v.BoxHeaderType = videoStream.inVideoStream.ReadChars(4);
						//TODO skip
						byte[] t = videoStream.inVideoStream.ReadBytes(61);
						
						temp.sampleDescriptionTable.Add(mp4v);
						
						log.Info("--mp4v sample item--");
						log.Debug("Size: " + mp4v.Size);
						log.Debug("DataFormat: " + new String(mp4v.DataFormat));
						log.Debug("Reserved: " + mp4v.Reserved);
						log.Debug("DataReferenceIndex: " + mp4v.DataReferenceIndex);
						log.Debug("Width: " + mp4v.Width);
						log.Debug("Height: " +mp4v.Height);
						log.Debug("BoxHeaderSize: " +mp4v.BoxHeaderSize);
						log.Debug("BoxHeaderType: " + new String(mp4v.BoxHeaderType));
						break;
					default:
						
						break;
				}
				
				
			}
			
			return temp;
		}
		
		public TimeToSampleAtom ReadStts(Atom atom){
			TimeToSampleAtom temp = new TimeToSampleAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----STTS----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("NumberOfEntries: " + temp.NumberOfEntries);
			
			for (int i = 0; i < temp.NumberOfEntries; i++) {
				TimeToSampleItem tempItem = new TimeToSampleItem();
				tempItem.SampleCount = videoStream.avio_rb32();
				tempItem.SampleDuration = videoStream.avio_rb32();
				temp.timeToSampleTable.Add(tempItem);
				
				log.Info("--Edit item--");
				log.Debug("SampleCount: " + tempItem.SampleCount);
				log.Debug("SampleDuration: " + tempItem.SampleDuration);
			}
			
			return temp;
		}
		
		public SampleToChunkAtom ReadStsc(Atom atom){
			SampleToChunkAtom temp = new SampleToChunkAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----STSC----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("NumberOfEntries: " + temp.NumberOfEntries);
			
			for (int i = 0; i < temp.NumberOfEntries; i++) {
				SampleToChunkItem tempItem = new SampleToChunkItem();
				tempItem.FirstChunck = videoStream.avio_rb32();
				tempItem.SamplesPerChunk = videoStream.avio_rb32();
				tempItem.SamplesDescription = videoStream.avio_rb32();
				temp.SampleToChunkTable.Add(tempItem);
				
				log.Info("--SampleToChunk item--");
				log.Debug("FirstChunck: " + tempItem.FirstChunck);
				log.Debug("SamplesPerChunk: " + tempItem.SamplesPerChunk);
				log.Debug("SamplesDescription: " + tempItem.SamplesDescription);
			}
			
			return temp;
		}
		
		public SampleSizeAtom ReadStsz(Atom atom){
			SampleSizeAtom temp = new SampleSizeAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.SampleSize = videoStream.avio_rb32();
			temp.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----STSZ----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("SampleSize: " + temp.SampleSize);
			log.Debug("NumberOfEntries: " + temp.NumberOfEntries);
			
			if(temp.SampleSize == 0){
				for (int i = 0; i < temp.NumberOfEntries; i++) {
					uint tempItem = videoStream.avio_rb32();
					temp.SampleSizeTable.Add(tempItem);
					
					log.Info("--SampleSizeAtom item--");
					log.Debug("value: " + tempItem);
				}
			}
			
			return temp;
		}
		
		public ChunkOffsetAtom ReadStco(Atom atom){
			ChunkOffsetAtom temp = new ChunkOffsetAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----STCO----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("NumberOfEntries: " + temp.NumberOfEntries);
			
			for (int i = 0; i < temp.NumberOfEntries; i++) {
				uint tempItem = videoStream.avio_rb32();
				temp.ChunkOffsetTable.Add(tempItem);
				
				log.Info("--ChunkOffsetAtom item--");
				log.Debug("value: " + tempItem);
			}
			
			return temp;
		}
		
		public SyncSampleAtom ReadStss(Atom atom){
			SyncSampleAtom temp = new SyncSampleAtom(atom);
			temp.Version = videoStream.avio_r8();
			temp.Flags = videoStream.inVideoStream.ReadBytes(3);
			temp.NumberOfEntries = videoStream.avio_rb32();
			
			log.Info("----STSS----");
			log.Debug("Size: " + temp.Size);
			log.Debug("Type: " + new String(temp.Type));
			log.Debug("Version: " + temp.Version);
			log.Debug("Flags: " + temp.Flags);
			log.Debug("NumberOfEntries: " + temp.NumberOfEntries);
			
			for (int i = 0; i < temp.NumberOfEntries; i++) {
				uint tempItem = videoStream.avio_rb32();
				temp.SyncSampleTable.Add(tempItem);
				
				log.Info("--SyncSampleTable item--");
				log.Debug("value: " + tempItem);
			}
			
			return temp;
		}
	}
}
